% This is a package which will contain the various generic
% commands that I would like to have available in most
% of my documents.

\RequirePackage{ifthen}

% These are temporary variables that I will use throughout
% my macros.
\newlength{\JP@templength}
\newcounter{JP@counter}

% \savelength will store the width of its argument in \JP@templength
\newcommand{\savelength}[1]{%
    \settowidth{\JP@templength}{#1}%
}

% \getlength will expand to \JP@templength
\newcommand{\getlength}{%
    \JP@templength
}

% The \replicate command repeats its first argument a number of times
% specified by its second argument.
%   For example, \replicate{food}{3} results in foodfoodfood
\newcommand{\replicate}[2]{%
    \setcounter{JP@counter}{#2}%
    \whiledo{\value{JP@counter}>0}{%
	#1\addtocounter{JP@counter}{-1}%
    }%
}
    
% This command will set line spacing.
% Usage: \setspacing{<line spacing>}
% If one uses this command in the body of the document, one
% needs to follow it with a font changing command for it to
% take effect.

\newcommand{\setspacing}[1]{\renewcommand{\baselinestretch}{#1}}

% These commands set the size of the margins, creating a packed
% page, full page, or medium page; which have 0.75 inch, 1 inch,
% and 1.25 inch margins on all sides, respectively.
% Usage: \packedmargins, \fullmargins, or \mediummargins
% Note that with \packedmargins you cannot use headers or footers, and
% with both \fullmargins you cannot use marginal notes.

\newcommand{\packedmargins}%
  	{\setlength{\textwidth}{7in}\setlength{\textheight}{9.5in}%
  	\setlength{\oddsidemargin}{-0.25in}\setlength{\evensidemargin}{-0.25in}%
  	\setlength{\topmargin}{-0.25in}\setlength{\headsep}{0in}%
  	\setlength{\headheight}{0in}\setlength{\footskip}{0in}%
  	\setlength{\marginparwidth}{0pt}\setlength{\marginparsep}{0pt}
	\pagestyle{empty}}

\newcommand{\fullmargins}%
  	{\setlength{\textwidth}{6.5in}\setlength{\textheight}{8.7in}%
  	\setlength{\oddsidemargin}{0in}\setlength{\evensidemargin}{0in}%
  	\setlength{\topmargin}{-0.5in}\setlength{\headsep}{20pt}%
  	\setlength{\headheight}{16pt}\setlength{\footskip}{0.4in}%
  	\setlength{\marginparwidth}{0.4in}\setlength{\marginparsep}{0.1in}}

\newcommand{\mediummargins}%
  	{\setlength{\textwidth}{6in}\setlength{\textheight}{8.2in}%
	\setlength{\oddsidemargin}{0.25in}\setlength{\evensidemargin}{0.25in}%
	\setlength{\topmargin}{-16pt}\setlength{\headsep}{23pt}%
	\setlength{\headheight}{14pt}\setlength{\footskip}{0.4in}}


% This command allows you to shift the output of each page both
% horizontally and vertically by adjusting the \hoffset and \voffset
% parameters. It should be used in the preamble.
% Usage: \pageoffset{<horizontal shift>}{<vertical shift>}
% For the HP LaserJet 6L, a horizontal offset of -0.25in centers
% the text on the page properly.

\newcommand{\pageoffset}[2]%
	{\setlength{\hoffset}{#1}\setlength{\voffset}{#2}}

% \pageshift works much like \pageoffset, except that instead of setting
% the values of \hoffset and \voffset to the values of the paramaters,
% it adds the parameters to the length of \hoffset and \voffset.

\newcommand{\pageshift}[2]%
	{\addtolength{\hoffset}{#1}\addtolength{\voffset}{#2}}

% These following commands create a new page style, mystyle, on
% which I have greater control of the headers and the footers.
% To use this style, enter \pagestyle{mystyle} in the document,
% and then use \myhead and \myfoot to set the header and footer.
% To align text within the header and footer, use \hfill's rather
% than any alignment command such as \centering.

\newcommand{\jp@myhead}{}
\newcommand{\jp@myfoot}{}

\newcommand{\myhead}[1]{\renewcommand{\jp@myhead}{#1}}
\newcommand{\myfoot}[1]{\renewcommand{\jp@myfoot}{#1}}

\newcommand{\ps@mystyle}{%
	\renewcommand{\@oddhead}{\jp@myhead}%
	\renewcommand{\@oddfoot}{\jp@myfoot}}


% I've defined a few commands to produce the difficult special
% characters, the tilde, caret, and backslash, respectively.

\newcommand{\mytilde}{\symbol{126}}
\newcommand{\mycaret}{\symbol{94}}
\newcommand{\mybackslash}{{\ttfamily \symbol{92}}}
\newcommand{\myplus}{\symbol{43}}
\newcommand{\myminus}{\ensuremath{-}}
\newcommand{\myequals}{\ensuremath{=}}
\newcommand{\mylessthan}{\ensuremath{<}}
\newcommand{\mygreaterthan}{\ensuremath{>}}
\newcommand{\myverticalbar}{\ensuremath{|}}

% Here are random special characters that I might want to use.
\newcommand{\degrees}{\symbol{"17}}

% These are some sectioning and spacing commands that I might use.

\newcommand{\mysection}[1]%
	{{\raggedright\Large\vspace{.25in}\textbf{#1}\vspace{.25in}}}

\newcommand{\mysmallspace}{\vspace{.25in}}

% \tabspace inserts a box that contains an amount of horizontal
% space equal to the current paragraph indentation; it can be
% used before the first paragraph of sections to indent the normally
% unindented text.

\newcommand{\tabspace}{\mbox{\indent}}

% If one is planning to use the typewriter fonts for extended sections
% of text, which is usually done with the \ttfamily declaration, TeX will
% create many overfull and underfull hboxes; to prevent overfull hboxes,
% and to prevent TeX and LaTeX from spitting out a glut of warning messages,
% insert a \verysloppy declaration at the beginning of the section of the
% document in which will reside the typewriter text.
% Usage: \verysloppy

\newcommand{\verysloppy}{\sloppy\hbadness=10000}

% By default, when the \ttfamily font command is used, hyphenation is
% disabled; to re-enable it, use \tthyphenate. To disable hyphenation once
% more, use \ttnohyphenate.
\newcommand{\tthyphenate}{\ttfamily\hyphenchar\font`\-}
\newcommand{\ttnohyphenate}{\ttfamily\hyphenchar \font\m@ne}

% If one is typesetting a very long paragraph, so long that it exceeds
% TeX's memory capacity, one can insert the \longparbreak command at
% various locations throughout the paragraph, every 50 lines or so of
% text.

\newcommand{\longparbreak}{{\parfillskip=0pt\par\parskip=0pt\noindent}}

% These are convenience macros for making text italic, bold, and underlined,
% respectively, so that I don't have to type the entire long macro name.
\newcommand{\ti}[1]{\textit{#1}}
\newcommand{\tb}[1]{\textbf{#1}}
\newcommand{\tu}[1]{\underline{#1}}

% \linespace inserts vertical space equal to its argument times
% the current \baselineskip; it is as though one hit return that
% number of times.
% \ttlinespace does purportedly the same thing, except by using
% the \replicate command; this works better for DVI to text translators,
% such as dvi2tty.
\newcommand{\linespace}[1]{\vspace*{#1\baselineskip}}
\newcommand{\ttlinespace}[1]{\replicate{\ \\}{#1}}

% \spaces inserts an amount of horizontal space as though one inserted
% a number of spaces equal to its argument
\newcommand{\spaces}[1]{%
    \settowidth{\JP@templength}{ }%
    \hspace*{#1\JP@templength}%
    }


% The plaintext environment allows a user to write text as though he were
% entering it into a straight ASCII formatter, rather than LaTeX; that is,
% I redefine `$,' `%,' `#,' `&,' and `"' so that they produce the
% characters one would expect, rather than being control characters. Also,
% I modify the definition of `-' so that the sequence `--' will yield an
% em-dash, and any other use of `-' will act as before.

% I found that even though the \def- is within a macro, the category code
% of `-' has to be active in order for it to parse correctly, so I take
% care to change the category code of `-' beforehand, define the macro,
% and then restore the old value afterwards. The situation is likewise for
% the `"' character.

% This is used to preserve category codes.
\newcount\oldcatcode

% \PT@dashpipe is used by the \PT@processdash macro to communicate with
% itself, when it is expanded at different points. When a dash is encountered
% and \PT@processdash is expanded, it checks to see if the next character is
% a dash; if so, it sets \PT@dashpipe to `B' and finishes without typesetting
% anything. Then, when the second dash is encountered, it sees that \PT@dashpipe
% is `B', typesets an em-dash, and sets \PT@dashpipe back to `A'.

\newcommand{\PT@dashpipe}{A}

\oldcatcode=\catcode`\-
\catcode`\-=13  % An active character.
\newcommand{\PT@remapdash}{%
    \catcode`\-=13
    \def\PT@processdash{%
	\if \PT@dashpipe B%
	    %\message{I am going to typeset an em dash!^^J}%
	    \textemdash%
	    \renewcommand{\PT@dashpipe}{A}%
	\else%
	    \ifx \PT@nexttoken -%
	        %\message{I see a dash in front of me.^^J}%
		\renewcommand{\PT@dashpipe}{B}%
	    \else%
		%\message{Oh, this is just a normal dash.^^J}%
	        \string-%   % Write a normal dash.
	    \fi%
	\fi}%
    \def-{\futurelet\PT@nexttoken\PT@processdash} }
\catcode`\-=\oldcatcode

\oldcatcode=\catcode`\"
\catcode`\"=13
\newcommand{\PT@remapquote}{%
    \catcode`\"=13
    \def"{\symbol{'175}} }
\catcode`\"=\oldcatcode

\newcommand{\PT@remapotherchars}{%
    \catcode`\$=12 \catcode`\%=12 \catcode`\#=12 \catcode`\&=12 }
    % Category code 12 is for `other characters.'

% If we want to have a section within a plaintext environment in which
% the character codes are at their default values, we can enclose it in
% braces, and use the \regchars declaration.
\newcommand{\PT@defregchars}{%
    \newcommand{\regchars}{%
        \catcode`\-=12 \catcode`\"=12
	\catcode`\$=3 \catcode`\%=14 \catcode`\#=6 \catcode`\&=4
	}
    }

\newenvironment{plaintext}%
    {\par
     \PT@remapdash
     \PT@remapquote
     \PT@remapotherchars
     \PT@defregchars}
    {\par}
